package com.data.basic.chapter03;

import com.data.basic.chapter02.Array;

import java.util.*;

/**
 * 数组队列的实现
 */
public class ArrayQueue<E> implements Queue<E> {
    private Array<E> array;

    public ArrayQueue() {
    array = new Array<E>();
    }

    public ArrayQueue(int capacity) {
        array = new Array<E>(capacity);
    }

    public void enqueue(E e) {
        array.addLast(e);

    }

    public E dequeue() {
        return array.removeFirst();
    }

    public E getFront() {
        return array.getFirst();
    }

    public int getSize() {
        return array.getSize();
    }

    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
            res.append("Queue：");
        res.append("front[");
        for (int i=0;i<array.getSize();i++){
            res.append(array.get(i));
            if (i!=array.getSize()-1)
                res.append(",");
        }
        res.append("]tail");
        return res.toString();
    }

    public static void main(String[] args) {
        ArrayQueue<Integer> arrayQueue = new ArrayQueue<Integer>();
        for (int i =0;i<10;i++){
            arrayQueue.enqueue(i);
            System.out.println(arrayQueue);
            if (i%3==2){
                arrayQueue.dequeue();
                System.out.println(arrayQueue);
            }
        }
    }
}
